var MUSIC_CLICK_BTN: string = "click_mp3";
var MUSIC_HIT: string = "hit_mp3";
var MUSIC_ADDNUM: string = "addNum_mp3";
var MUSIC_FIRE: string = "fire_mp3";
var MUSIC_FLOOR: string = "floor_mp3";
var MUSIC_BG: string = "background_mp3";

var EVENT_FIRE: string = "event fire";
var EVENT_REMOVE: string = "event remove";

var TYPE_HAND: string = "type hand";

var ICON_ADD: string = "addIcon_png"

var SHAPE = { rect: "rect", circle: "circle" }//形状

var OVER_DIS: number = 200;

class GameControl extends moon.BasicGamePanel {
    private timer: egret.Timer;//时间计时器
    private timertime: egret.Timer;//时间计时器
    private world: P2World;//2D物理世界
    private material: p2.Material;//碰撞时的弹性变化
    private names: string[] = [];//数字刚体皮肤名字
    private gun: Gun;//炮
    private controlGun: control.ControlBasic;//控制炮的方向
    private vec: egret.Point;//发炮时的冲量值
    private removeSkins: any[] = [];//需要删除刚体的皮肤
    private container: Sprite;//数字刚体皮肤容器
    private minBodyCount: number = 1;//数字刚体的最少数值
    private fakeBullet: any;
    private zuqiu: any[] = [];
    private my_ball: any[] = [];
    public isHoled: boolean = false;//是否进洞
    public isStopPanel: boolean = false;//是否掉结束界面
    public static motherBall: p2.Body;//母球刚体
    public static multiple: number;//力倍数
    private armature: any;
    private shape = new egret.Shape();
    private white: any;
    protected render(): void {
        super.render();
        // trace("v1.1");
        GameData.stageHeight = this.stageHeight;
        GameData.stageWidth = this.stageWidth;
        SoundControl.getIns().addItem(MUSIC_HIT);
        SoundControl.getIns().addItem(MUSIC_ADDNUM);
        SoundControl.getIns().addItem(MUSIC_FIRE);
        SoundControl.getIns().addItem(MUSIC_FLOOR);
        SoundControl.getIns().addItem(MUSIC_BG, true);
        SoundControl.getIns().play(MUSIC_BG, 0, 9999);




        this.createImageBg("ditu_png");
        var world: P2World = new P2World(0, 200);
        this.addChild(world);
        this.world = world;
        world.loopBackFun = this.loopP2World.bind(this);
        /**分数 */
        this.txtMyScore = this.createText(450, 50);
        this.updateScore();
        /**显示 */
        this.txtLevel = this.createText(250, 550);
        /**计时器 */
        this.txtTime = this.createText(250, 0);
        // this.updateTime();
        // GameControl.Maxtime = 100;
        // this.time = moon.BasicGameStart.mavti;
        this.time = 120;
        this.material = new p2.Material();

        //创建左右刚体
        var leftTop = this.createBody2("zuoshang1_png");
        leftTop.position = [63, 308];
        var leftTop1 = new MImage("zuoshang_png");
        leftTop1.anchorOffsetX = leftTop1.width / 2;
        leftTop1.anchorOffsetY = leftTop1.height / 2;
        leftTop1.x = 63;
        leftTop1.y = 308;
        this.addChild(leftTop1);

        var leftDown = this.createBody2("zuoshang1_png");
        leftDown.position = [63, 825];
        var leftTop1 = new MImage("zuoshang_png");
        leftTop1.anchorOffsetX = leftTop1.width / 2;
        leftTop1.anchorOffsetY = leftTop1.height / 2;
        leftTop1.x = 63;
        leftTop1.y = 825;
        this.addChild(leftTop1);

        var rightTop = this.createBody2("youshang1_png");
        rightTop.position = [this.getRight(67), 306];
        var rightTop1 = new MImage("youshang_png");
        rightTop1.anchorOffsetX = rightTop1.width / 2;
        rightTop1.anchorOffsetY = rightTop1.height / 2;
        rightTop1.x = this.getRight(67);
        rightTop1.y = 306;
        this.addChild(rightTop1);

        var rightDown = this.createBody2("youshang1_png");
        rightDown.position = [this.getRight(67), 825];
        var rightTop1 = new MImage("youshang_png");
        rightTop1.anchorOffsetX = rightTop1.width / 2;
        rightTop1.anchorOffsetY = rightTop1.height / 2;
        rightTop1.x = this.getRight(67);
        rightTop1.y = 825;
        this.addChild(rightTop1);

        //创建顶部底部刚体
        var top = this.createBody2("shang1_png");
        top.position = [322, 52];
        var top1 = new MImage("shang_png");
        top1.anchorOffsetX = top1.width / 2;
        top1.anchorOffsetY = top1.height / 2;
        top1.x = 322;
        top1.y = 52;
        this.addChild(top1);
        var down = this.createBody2("xia1_png");
        down.position = [315, 1084.5];
        var down1 = new MImage("xia_png");
        down1.anchorOffsetX = down1.width / 2;
        down1.anchorOffsetY = down1.height / 2;
        down1.x = 315;
        down1.y = 1084.5;
        this.addChild(down1);

        //动画
        // this.Init();
        // this.createArmation("qiu_2");
        //创建球洞

        var cun_dong = [];
        var cun_dong2 = [];
        for (var i: number = 0; i < 3; i++) {
            var dong = this.createBody2("heidong02_png");
            var dong2 = new MImage("heidong01_png");
            this.addChild(dong2);
            dong2.anchorOffsetX = dong2.width / 2;
            dong2.anchorOffsetY = dong2.height / 2;
            dong.userData["dong"] = true;
            dong.position = [50, 65 + 510 * i];
            // dong2.x = 53.5;
            // dong2.y = 50 + 510 * i;
            cun_dong.push(dong2);

        }
        cun_dong[0].x = 53.5;
        cun_dong[0].y = 50;
        cun_dong[1].x = 40;
        cun_dong[1].y = 565;
        cun_dong[2].x = 53.5;
        cun_dong[2].y = 1083;


        for (var i: number = 0; i < 3; i++) {
            var dong = this.createBody2("heidong02_png");
            var dong2 = new MImage("heidong01_png");
            this.addChild(dong2);
            dong2.anchorOffsetX = dong2.width / 2;
            dong2.anchorOffsetY = dong2.height / 2;
            dong.userData["dong"] = true;
            dong.position = [590, 62 + 510 * i];

            cun_dong2.push(dong2);
        }
        cun_dong2[0].x = 585;
        cun_dong2[0].y = 50;
        cun_dong2[1].x = 595;
        cun_dong2[1].y = 565;
        cun_dong2[2].x = 585;
        cun_dong2[2].y = 1083;



        //摆球

        this.baiBall();

        //给所有球加标签
        this.zuqiu[0].userData["taiqiu0"] = true;//标签
        this.zuqiu[1].userData["taiqiu1"] = true;
        this.zuqiu[2].userData["taiqiu2"] = true;
        this.zuqiu[3].userData["taiqiu3"] = true;
        this.zuqiu[4].userData["taiqiu4"] = true;
        this.zuqiu[5].userData["taiqiu5"] = true;
        this.zuqiu[6].userData["taiqiu6"] = true;
        this.zuqiu[7].userData["taiqiu7"] = true;
        this.zuqiu[8].userData["taiqiu8"] = true;
        this.zuqiu[9].userData["taiqiu9"] = true;
        this.zuqiu[10].userData["taiqiu10"] = true;
        this.zuqiu[11].userData["taiqiu11"] = true;
        this.zuqiu[12].userData["taiqiu12"] = true;
        this.zuqiu[13].userData["taiqiu13"] = true;
        this.zuqiu[14].userData["taiqiu14"] = true;
        //更改所有球皮肤
        this.zuqiu[0].userData.skin = this.createSkin("chen1_png");//皮肤
        this.zuqiu[1].userData.skin = this.createSkin("hei_png");
        this.zuqiu[2].userData.skin = this.createSkin("hong_png");
        this.zuqiu[3].userData.skin = this.createSkin("hong1_png");
        this.zuqiu[4].userData.skin = this.createSkin("huang1_png");
        this.zuqiu[5].userData.skin = this.createSkin("huang02_png");
        this.zuqiu[6].userData.skin = this.createSkin("juhong_png");
        this.zuqiu[7].userData.skin = this.createSkin("lan_png");
        this.zuqiu[8].userData.skin = this.createSkin("lanlu_png");
        this.zuqiu[9].userData.skin = this.createSkin("lu_png");
        this.zuqiu[10].userData.skin = this.createSkin("shenlanse_png");
        this.zuqiu[11].userData.skin = this.createSkin("yingguanglu1_png");
        this.zuqiu[12].userData.skin = this.createSkin("zi_png");
        this.zuqiu[13].userData.skin = this.createSkin("zihong_png");
        this.zuqiu[14].userData.skin = this.createSkin("chen1_png");

        //母球
        this.createMother();

        // //创建炮
        // var gun: Gun = new Gun;
        // gun.addEvent(EVENT_FIRE, this.onFire, this)
        // this.addChild(gun);
        // this.gun = gun;
        // gun.x = GameControl.motherBall.position[0];
        // gun.y = GameControl.motherBall.position[1];
        // console.log(Gun.distance + " ++++++");
        this.container = new Sprite;
        this.addChild(this.container);
        var conMask: Sprite = moon.MoonUI.getRect(GameData.stageWidth, GameData.stageHeight - 200, 0, 0, 160);
        this.container.mask = conMask;

        var controlGun: control.ControlBasic = new control.ControlBasic(this.stage);
        //controlGun.open();
        controlGun.startBackFun = this.startBackFun.bind(this);
        controlGun.moveBackFun = this.controlMove.bind(this);
        controlGun.endBackFun = this.endBackFun.bind(this);
        this.controlGun = controlGun;

        world.p2World.on("beginContact", this.onHitBegin.bind(this));


    }
    //动画
    private Init() {
        var ske = RES.getRes("qiu_2_ske_json");
        var tex = RES.getRes("qiu_2_tex_json");
        var texture = RES.getRes("qiu_2_tex_png");

        var factory = new dragonBones.EgretFactory();
        factory.addDragonBonesData(factory.parseDragonBonesData(ske));
        factory.addTextureAtlasData(factory.parseTextureAtlasData(tex, texture));

        this.armature = factory.buildArmature("qiu_2");
        var armatureDisplay = this.armature.display;
        armatureDisplay.x = 300;
        armatureDisplay.y = 600;
        this.armature.clock = factory.clock;


    }
    //生成动作
    private createArmation(armationName: string, playTimes?: number) {
        this.armature.animation.play(armationName, playTimes);
        this.armature.animation.timeScale = 1;

    }
    private stopArmation() {
        this.armature.animation.reset();
    }
    //创建母球
    public createMother(): void {
        GameControl.motherBall = this.createBody("bai_png");
        GameControl.motherBall.userData["motherhit"] = true;
        GameControl.motherBall.position = [320, 850];

    }
    //摆球
    public baiBall(): void {
        const r: number = 40;//台球的直径
        var b: number = 1;
        var maxR: number = 5 * r;//最大的一排的总直径
        for (var i: number = 5; i > 0; i--) {
            b++;
            for (var j: number = 0; j < i; j++) {
                var sp = this.createBody("shenlanse_png");
                //      console.log("小球的质量是" + sp.mass);
                sp.position = [250 + (maxR - (i * r)) / 2 + j * r, 100 + b * r];
                sp.damping = 0.6;
                this.zuqiu.push(sp);
                this.my_ball.push(sp);
            }
        }
    }
    protected initView(): void {
        //需要复写
    }
    public initGame(): void {
        this.updateTime();
        this.myScore = 0;
        var str2: any = "";
        this.level = <number>str2;
        this.updateScore();//分数
        this.controlGun.open();
        this.ganShow();


        // //创建炮
        // var gun: Gun = new Gun;
        // gun.addEvent(EVENT_FIRE, this.onFire, this)
        // this.addChild(gun);
        // this.gun = gun;
        // gun.x = GameControl.motherBall.position[0];
        // gun.y = GameControl.motherBall.position[1];
    }
    /**复活 */
    public revive(): void {
        //  this.moveNum = 0;
        //   this.ballNum = 0;
        //  this.play();
        //  this.controlGun.open();
    }
    protected setBottom(height: number, distance: number): number {
        return GameData.stageHeight - height - distance;
    }
    protected moveNumBody(): void {
        // var bodys = this.world.p2World.bodies;
        // var l: number = bodys.length;
        // for (var i: number = 0; i < l; i++) {
        //     var body: p2.Body = bodys[i];
        //     if (body.userData && body.userData.skin) {
        //         if (body.userData["num"]) {
        //             body.position[1] -= 10;
        //             if (body.position[1] <= OVER_DIS) {
        //                 if (body.userData.skin.imageName != ICON_ADD) {
        //                     this.over();
        //                     return;
        //                 }
        //             }
        //         }
        //     }
        // }

    }
    protected over(): void {
        super.over();
        //this.removeChild(new GameMain);
        // this.gun.addEvent(EVENT_FIRE, this.onFire, this)
        this.isStopPanel = true;
        SoundControl.getIns().stop(MUSIC_FIRE);
        this.gun.removeFromParent();
        this.controlGun.close();
        // console.log("jishiqian" + this.timer.running);
        // if (this.timer != null) {
        // this.timer.stop();
        this.timertime.stop();
        // this.timer = null;
        // }
        //console.log("jishihou" + this.timer.running);

        SoundControl.getIns().stop(MUSIC_BG);
        var bodys = this.world.p2World.bodies;
        for (var i: number = 0; i < bodys.length; i++) {
            var body: p2.Body = bodys[i];
            if (body.userData && body.userData.skin) {
                //if (body.userData["num"]) 
                {
                    var skin: MImage = body.userData.skin;
                    this.world.removeBodys.push(body);
                    skin.removeFromParent(true);
                }
            }
        }
    }
    protected updateNumBody(): void {

    }
    protected onHitImpact(evt): void {
        // var ball: p2.Body;
        // if (evt.bodyA.userData && evt.bodyB.userData) {
        //     if (evt.bodyA.userData["ball"]) ball = evt.bodyA;
        //     if (evt.bodyB.userData["ball"]) ball = evt.bodyB;
        //     //与数字刚体
        //     if (evt.bodyA.userData["ball"] && evt.bodyB.userData["num"] || evt.bodyB.userData["ball"] && evt.bodyA.userData["num"]) {
        //         var vec = new Point(ball.velocity[0], ball.velocity[1]);
        //         var vecnum: number = Math.sqrt(vec.x * vec.x + vec.y * vec.y);
        //         console.log(vecnum);
        //         if (vecnum < 50) {
        //             this.setBallImpulse(ball);
        //         }
        //     }
        // }
    }
    //碰撞后事件
    protected onHitBegin(evt): void {


        var mother_ball: p2.Body;
        if (evt.bodyA.userData["motherhit"]) mother_ball = evt.bodyA;
        if (evt.bodyB.userData["motherhit"]) mother_ball = evt.bodyB;
        if (evt.bodyA.userData["dong"] && evt.bodyB.userData["motherhit"] || evt.bodyB.userData["dong"] && evt.bodyA.userData["motherhit"]) {
            if (mother_ball) {
                this.myScore -= 20;//白球进洞分数
                var str2: any = "白球进洞-20分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = mother_ball.userData.skin;
                this.world.removeBodys.push(mother_ball);
                skin.removeFromParent(true);
                this.isHoled = true;
                // this.over();
            }
            if (this.isHoled) {
                this.timer = new egret.Timer(3000, 1);
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    //     console.log(this.time);
                    if (this.time != 0) {
                        var whiteBall = new MImage("bai_png")
                        whiteBall.x = 320;
                        whiteBall.y = 850;
                        this.addChild(whiteBall);
                        this.white = whiteBall;

                        //  this.createMother();//两秒之后再添加一个

                        this.createGameScene();//添加拖动球
                    }

                }, this);
                this.timer.start();
                this.removeChild(this.gun);
                this.controlGun.close();//关闭杆，线，围绕白球旋转
            }
        }
        //删除台球
        var taiqiu0: p2.Body;
        if (evt.bodyA.userData["taiqiu0"]) taiqiu0 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu0"]) taiqiu0 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu0"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu0"] && evt.bodyA.userData["dong"]) {
            console.log("00000");
            if (taiqiu0) {
                this.myScore += 10;//分数
                var str2: any = "黄球进洞+10分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu0.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu0);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu1: p2.Body;
        if (evt.bodyA.userData["taiqiu1"]) taiqiu1 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu1"]) taiqiu1 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu1"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu1"] && evt.bodyA.userData["dong"]) {
            console.log("10000");
            if (taiqiu1) {
                this.myScore += 13;//分数
                var str2: any = "蓝球进洞+13分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu1.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu1);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu2: p2.Body;
        if (evt.bodyA.userData["taiqiu2"]) taiqiu2 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu2"]) taiqiu2 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu2"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu2"] && evt.bodyA.userData["dong"]) {
            console.log("200000");
            if (taiqiu2) {
                this.myScore += 16;//分数
                var str2: any = "绿球进洞+16分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu2.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu2);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu3: p2.Body;
        if (evt.bodyA.userData["taiqiu3"]) taiqiu3 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu3"]) taiqiu3 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu3"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu3"] && evt.bodyA.userData["dong"]) {
            console.log("300000");
            if (taiqiu3) {
                this.myScore += 19;//分数
                var str2: any = "紫球进洞+19分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu3.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu3);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu4: p2.Body;
        if (evt.bodyA.userData["taiqiu4"]) taiqiu4 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu4"]) taiqiu4 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu4"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu4"] && evt.bodyA.userData["dong"]) {
            console.log("400000");
            if (taiqiu4) {
                this.myScore += 21;//分数
                var str2: any = "黑紫球进洞+21分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu4.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu4);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu5: p2.Body;
        if (evt.bodyA.userData["taiqiu5"]) taiqiu5 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu5"]) taiqiu5 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu5"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu5"] && evt.bodyA.userData["dong"]) {
            console.log("500000");
            if (taiqiu5) {
                this.myScore += 11;//分数
                var str2: any = "红球进洞+11分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu5.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu5);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu6: p2.Body;
        if (evt.bodyA.userData["taiqiu6"]) taiqiu6 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu6"]) taiqiu6 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu6"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu6"] && evt.bodyA.userData["dong"]) {
            console.log("600000");
            if (taiqiu6) {
                this.myScore += 12;//分数
                var str2: any = "蓝球进洞+12分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu6.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu6);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu7: p2.Body;
        if (evt.bodyA.userData["taiqiu7"]) taiqiu7 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu7"]) taiqiu7 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu7"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu7"] && evt.bodyA.userData["dong"]) {
            console.log("700000");
            if (taiqiu7) {
                this.myScore += 14;//分数
                var str2: any = "青球进洞+14分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu7.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu7);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu8: p2.Body;
        if (evt.bodyA.userData["taiqiu8"]) taiqiu8 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu8"]) taiqiu8 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu8"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu8"] && evt.bodyA.userData["dong"]) {
            console.log("800000");
            if (taiqiu8) {
                this.myScore += 15;//分数
                var str2: any = "紫球进洞+15分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu8.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu8);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu9: p2.Body;
        if (evt.bodyA.userData["taiqiu9"]) taiqiu9 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu9"]) taiqiu9 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu9"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu9"] && evt.bodyA.userData["dong"]) {
            console.log("900000");
            if (taiqiu9) {
                this.myScore += 17;//分数
                var str2: any = "草球进洞+17分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu9.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu9);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu10: p2.Body;
        if (evt.bodyA.userData["taiqiu10"]) taiqiu10 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu10"]) taiqiu10 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu10"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu10"] && evt.bodyA.userData["dong"]) {
            console.log("0100000");
            if (taiqiu10) {
                this.myScore += 10;//分数
                var str2: any = "黑球进洞+10分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu10.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu10);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu11: p2.Body;
        if (evt.bodyA.userData["taiqiu11"]) taiqiu11 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu11"]) taiqiu11 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu11"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu11"] && evt.bodyA.userData["dong"]) {
            console.log("1100000");
            if (taiqiu11) {
                this.myScore += 12;//分数
                var str2: any = "粉球进洞+12分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu11.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu11);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu12: p2.Body;
        if (evt.bodyA.userData["taiqiu12"]) taiqiu12 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu12"]) taiqiu12 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu12"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu12"] && evt.bodyA.userData["dong"]) {
            console.log("1200000");
            if (taiqiu12) {
                this.myScore += 15;//分数
                var str2: any = "荧球进洞+15分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu12.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu12);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu13: p2.Body;
        if (evt.bodyA.userData["taiqiu13"]) taiqiu13 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu13"]) taiqiu13 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu13"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu13"] && evt.bodyA.userData["dong"]) {
            console.log("1300000");
            if (taiqiu13) {
                this.myScore += 19;//分数
                var str2: any = "黄球进洞+19分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu13.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu13);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }
        var taiqiu14: p2.Body;
        if (evt.bodyA.userData["taiqiu14"]) taiqiu14 = evt.bodyA;
        if (evt.bodyB.userData["taiqiu14"]) taiqiu14 = evt.bodyB;
        if (evt.bodyA.userData["taiqiu14"] && evt.bodyB.userData["dong"] || evt.bodyB.userData["taiqiu14"] && evt.bodyA.userData["dong"]) {
            console.log("1400000");
            if (taiqiu14) {
                this.myScore += 11;//分数
                var str2: any = "蓝球进洞+11分";
                this.level = <number>str2;//显示
                this.updateScore();
                this.updateLevel();
                this.timer = new egret.Timer(1000, 1);//显示一秒钟后消失
                this.timer.addEventListener(egret.TimerEvent.TIMER, e => {
                    this.removeChild(this.txtLevel);
                }, this);
                this.timer.start();
                this.addChild(this.txtLevel);
                var skin: NumImage = taiqiu14.userData.skin;
                //移除障碍物刚体
                this.world.removeBodys.push(taiqiu14);
                //移除障碍物皮肤
                skin.removeFromParent(true);

            }
        }



    }
    protected setBallImpulse(ball: p2.Body): void {
        var x = Math.random() > 0.5 ? -5000 : 5000;
        var y = -5000 - (Math.random() * 2000);
        ball.applyImpulse([x, y], [0, 0]);
    }
    //分数
    protected updateScore(): void {
        if (this.myScore < 0) {
            this.myScore = 0;
        }
        this.txtMyScore.text = "得分:" + this.myScore;
        this.txtMyScore.x = 400;
        this.txtMyScore.y = 75;

    }
    //提示面板
    protected updateLevel(): void {

        this.txtLevel.text = this.level + "";
    }

    /**计时器 */
    protected updateTime(): void {
        if (!this.isStopPanel) {
            //       console.log(this.time);
            let timer = new egret.Timer(1000, 0);
            timer.addEventListener(egret.TimerEvent.TIMER, e => {
                this.time--;
                this.txtTime.text = "时间:" + this.time;
                if (this.time <= 0) {
                    this.time = 0;
                    this.txtTime.text = "时间:" + this.time;
                    var a: any = String(this.myScore);
                    var KVData = [{ key: "score", value: a }];//获取玩家游戏中的分数
                    egretToWX.getInstance().storageUserData(KVData);//发送分说至排行榜
                    this.updateScore();
                    timer.stop();
                    this.over();
                    this.controlGun.close();
                }
            }, this);
            timer.start();

        }
        this.creatGun();
    }
    private creatGun() {
        //创建炮
        var gun: Gun = new Gun;
        gun.addEvent(EVENT_FIRE, this.onFire, this)
        this.addChild(gun);
        this.gun = gun;
        gun.x = GameControl.motherBall.position[0];
        gun.y = GameControl.motherBall.position[1];
        //   console.log("cw");
    }
  


    /**创建刚体白球拖动 */
    private createGameScene(): void {
        // //转换比例
        var factor: number = 1;
        var p2World: p2.World = new p2.World();

        //添加拖动事件
        this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, moveBegin, this);
        this.stage.addEventListener(egret.TouchEvent.TOUCH_END, moveEnd, this);
        let data = this;
        let time = new egret.Timer(3000, 1);
        time.addEventListener(egret.TimerEvent.TIMER_COMPLETE, e => {
            data.stage.removeEventListener(egret.TouchEvent.TOUCH_END, moveEnd, data);//删除鼠标离开
            data.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, onMove, data);//删除鼠标移动
            data.stage.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, moveBegin, data);


            //    console.log(this.white.position);
            let x = this.white.x;
            let y = this.white.y;
            this.removeChild(this.white);
            this.createMother();
            GameControl.motherBall.position[0] = x+20;
            GameControl.motherBall.position[1] = y+20;
            this.creatGun();


            // data.gun.x = GameControl.motherBall.position[0];
            // data.gun.y = GameControl.motherBall.position[1];
            data.gun.showLine();
            data.controlGun.open();
        }, data);
        time.start();
        var offsetX, offsetY;
        var prePosX, prePosY;
        //鼠标点击
        function moveBegin(event) {

            time.stop();
            this.stage.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, moveBegin, this);//删除鼠标点击
            this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, onMove, this);
            prePosX = event.stageX;
            prePosY = event.stageY;
            //this.Qui.type = p2.Body.KINEMATIC;
        }
        //鼠标移动。拖动白球
        function onMove(event) {
            offsetX = event.stageX - prePosX;
            offsetY = event.stageY - prePosY;

            // GameControl.motherBall.position[0] += offsetX / factor;
            // GameControl.motherBall.position[1] += offsetY / factor;

            this.white.x += offsetX / factor;
            this.white.y += offsetY / factor;

            //  console.log(this.Qui.sleepState);

            prePosX = event.stageX;
            prePosY = event.stageY;

            // GameControl.motherBall.type=p2.Body.SLEEPY;
            // GameControl.motherBall.sleepTimeLimit=3000;
            // rectDisplayObject.x = event.stageX;
            // rectDisplayObject.y = event.stageY;
        }
        //鼠标离开
        function moveEnd() {
  
            this.gun.showLine();
            let time = new egret.Timer(100, 1);
            time.addEventListener(egret.TimerEvent.TIMER_COMPLETE, e => {
                let x = this.white.x;
                let y = this.white.y;
                this.removeChild(this.white);
                this.createMother();
                GameControl.motherBall.position[0] = x+20;
                GameControl.motherBall.position[1] = y+20;
                this.creatGun();
                this.controlGun.open();

            }, data);
            time.start();

            this.stage.removeEventListener(egret.TouchEvent.TOUCH_END, moveEnd, this);//删除鼠标离开
            this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, onMove, this);//删除鼠标移动
            GameControl.motherBall.type = p2.Body.DYNAMIC;
            //this.controlGun.open();//开启杆，线，围绕白球旋转
        }
    }



    protected startBackFun(pos: egret.Point): void {
        this.gun.showLine();
        this.controlGun.close();
        this.controlMove(pos);
    }

    /**找到最近的球 */
    public getRaycastProps(startx: number, starty: number, endx: number, endy: number): p2.Body {
        let mindis = 30;
        let min = 1000;
        let nearestBall = null;
        for (let i = 0; i < this.my_ball.length; ++i) {
            let ball = this.my_ball[i];
            let zuobiao1 = new egret.Point(this.gun.x, this.gun.y);
            let zuobiao2 = new egret.Point(this.my_ball[i].position[0], this.my_ball[i].position[1]);
            let juli = egret.Point.distance(zuobiao1, zuobiao2);
            let dis = py2Obj.MathUtils.getDistanceBetweenPointAndLine(startx, starty, endx, endy, ball.position[0], ball.position[1], this);
            //   console.log("XXXXXXXXXXXXXXXXXXXX" + dis);
            if (dis < 30) {
                if (juli < min) {
                    min = juli;
                    nearestBall = ball;
                }
            }

        }
        if (nearestBall != null) {
            console.log("mindis" + mindis + "zuobiao" + nearestBall.position[0] + "," + nearestBall.position[1]);
        }
        return nearestBall;
    }
    protected controlMove(posMove: egret.Point): void {
        var pos = new egret.Point(-(posMove.x - this.gun.x), -(posMove.y - this.gun.y));
        //traceSimple(pos.x,pos.y);
        this.gun.updatePos(pos);


        let ptFrom = { x: this.gun.x, y: this.gun.y };
        let ptTo = { x: 0, y: 0 };
        var angle: number = Math.atan2(pos.x, pos.y)
        ptTo = { x: ptFrom.x + Math.sin(angle) * 1000, y: ptFrom.y + Math.cos(angle) * 1000 };
        let ray = new p2.Ray({ from: [ptFrom.x, ptFrom.y], to: [ptTo.x, ptTo.y], mode: p2.Ray.CLOSEST });


        let nearstBall = this.getRaycastProps(ptFrom.x, ptFrom.y, ptTo.x, ptTo.y);
        if (nearstBall != null) {
            let nextPos = { x: ptFrom.x, y: ptFrom.y };
            let dis = 1000;

            let bool = false;
            while (dis > 30) {
                if (egret.Point.distance(egret.Point.create(ptFrom.x, ptFrom.y), egret.Point.create(nextPos.x, nextPos.y)) > 1100) {
                    bool = true;

                    break;
                }
                nextPos = { x: nextPos.x + ray.direction[0] * 8.333333446434443, y: nextPos.y + ray.direction[1] * 8.333333446434443 };
                dis = Math.sqrt((nextPos.x - nearstBall.position[0]) * (nextPos.x - nearstBall.position[0]) + (nextPos.y - nearstBall.position[1]) * (nextPos.y - nearstBall.position[1]));

            }
            let ptIns = { x: nextPos.x, y: nextPos.y };
            this.shape.graphics.clear();

            //  var shape = new egret.Shape();
            //  this.shape.graphics.lineStyle(2, 0x00ff00);

            //出发的线
            // this.shape.graphics.moveTo(ptFrom.x, ptFrom.y);
            // this.shape.graphics.lineTo(ptTo.x, ptTo.y);
            if (bool == false) {
                //预测碰撞位置 
                this.shape.graphics.lineStyle(3, 0xf8f8ff);
                this.shape.graphics.moveTo(GameControl.motherBall.position[0], GameControl.motherBall.position[1]);
                this.shape.graphics.lineTo(ptIns.x, ptIns.y);
                this.shape.graphics.drawCircle(ptIns.x, ptIns.y, 15);
                //球心联系
                this.shape.graphics.moveTo(ptIns.x, ptIns.y);
                this.shape.graphics.lineTo(nearstBall.position[0], nearstBall.position[1]);
                //  console.log("|||||||||||||||**************" + ptIns1.x +"," +ptIns1.y + "V" + + ptIns2.x +"," +ptIns2.y );
                let rayOut = p2.vec2.clone([0, 0]);
                let rayDir = p2.vec2.clone([0, 0]);
                p2.vec2.normalize(rayDir, [ptFrom.x - ptTo.x, ptFrom.y - ptTo.y]);
                let rayNor = p2.vec2.clone([0, 0]);
                p2.vec2.normalize(rayNor, [ptIns.y - nearstBall.position[1], -ptIns.x + nearstBall.position[0]]);
                p2.vec2.reflect(rayOut, rayDir, rayNor);

                //   console.log(rayOut[0] + "{{{{{{{{{{{{{{{{{{{{}}}}}}}}}}}}}}}}}}}}" + rayOut[1]);
                //垂线
                this.shape.graphics.moveTo(ptIns.x, ptIns.y);
                if (ptIns.x >= nearstBall.position[0]) {
                    //       console.log("zheng zheng zheng right");

                    this.shape.graphics.lineTo(ptIns.x + rayNor[0] * 50, ptIns.y + rayNor[1] * 50);

                }
                else if (ptIns.x < nearstBall.position[0]) {
                    //     console.log("fu fu fu left");

                    this.shape.graphics.lineTo(ptIns.x + rayNor[0] * -50, ptIns.y + rayNor[1] * -50);
                }

            }
            //反射线
            //    this.shape.graphics.moveTo(ptIns.x, ptIns.y);
            // this.shape.graphics.lineTo(ptIns.x +rayOut[0]*500 ,ptIns.y +rayOut[1] *500 );
            //   this.shape.graphics.lineStyle(2, 0x0000ff);
            //入射线
            //  this.shape.graphics.moveTo(ptIns.x, ptIns.y);
            //  this.shape.graphics.lineTo(ptIns.x +ray.direction[0] *100 ,ptIns.y +ray.direction[1] *100 );
            //   this.shapeendFill();
            this.shape.graphics.endFill();
            this.addChild(this.shape);
        }
        else {
            this.shape.graphics.clear();
        }
    }
    protected endBackFun(posEnd: egret.Point): void {

        var pos = new egret.Point(posEnd.x - this.gun.x, posEnd.y - this.gun.y);
        var angle: number = Math.atan2(pos.x, pos.y)
        var dis: number = 10000;
        var x: number = Math.ceil(Math.sin(angle) * dis);
        var y: number = Math.ceil(Math.cos(angle) * dis);
        this.vec = new egret.Point(x, y);
        this.gun.hideLine();
        this.gun.fire();
        this.shape.graphics.clear();
        this.controlGun.close();


    }
    /**设置力 */
    public setForce(x, y): void {
        GameControl.motherBall.fixedX = false;
        GameControl.motherBall.fixedY = false;
        GameControl.motherBall.applyImpulse([x, y], [0, 0]);
    }
    /**设置球的位置 */
    public setPostion(x, y) {
        GameControl.motherBall.position = [x, y];

    }
    /**给球力 */
    private send(x, y, forcex, forcey) {


        GameControl.motherBall.type = 1;
        GameControl.motherBall.velocity = [0, 0];
        GameControl.motherBall.damping = 0.5;
        this.doMaterial(GameControl.motherBall);
        this.setPostion(x, y);
        this.setForce(forcex, forcey);

    }
    /**开火 */
    private onFire(e: MoonEvent): void {

        // var i = 0;
        // var arr = [];
        // var brr = [];
        if (Gun.distance >= 165) {
            Gun.distance = 165;
            this.send(this.gun.x, this.gun.y, -(this.vec.x * 0.007 * Gun.distance), -(this.vec.y * 0.007 * Gun.distance));
        }
        else {
            this.send(this.gun.x, this.gun.y, -(this.vec.x * 0.007 * Gun.distance), -(this.vec.y * 0.007 * Gun.distance));
        }
        //    console.log(Gun.distance + " ++++++");
        // this.timertime = new egret.Timer(3000, 0);
        // this.timertime.addEventListener(egret.TimerEvent.TIMER, e => {
        //     var a: number = Math.round(GameControl.motherBall.position[0]);
        //     var b: number = Math.round(GameControl.motherBall.position[1]);
        //     arr.push(a);
        //     brr.push(b);
        //     var c = Math.abs(arr[i - 1] - arr[i]);
        //     var d = Math.abs(brr[i - 1] - brr[i]);
        //     if (c < 10 && d < 10) {

        //         console.log("相等了");
        //         this.gun.x = arr[i];
        //         this.gun.y = brr[i];
        //         console.log("枪的位置" + this.gun.x, this.gun.y);
        //         console.log("母球的位置" + GameControl.motherBall.position);
        //         this.gun.showLine();
        //         this.controlGun.open();

        //     }
        //     i++;

        // }, this);
        // this.timertime.start();

        // this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.controlGanMove, this);



    }
    private ganShow() {
        var i = 0;
        var arr = [];
        var brr = [];
        this.timertime = new egret.Timer(3000, 0);
        this.timertime.addEventListener(egret.TimerEvent.TIMER, e => {
            var a: number = Math.round(GameControl.motherBall.position[0]);
            var b: number = Math.round(GameControl.motherBall.position[1]);
            arr.push(a);
            brr.push(b);
            var c = Math.abs(arr[i - 1] - arr[i]);
            var d = Math.abs(brr[i - 1] - brr[i]);
            if (c < 10 && d < 10) {

                //    console.log("相等了");
                this.gun.x = arr[i];
                this.gun.y = brr[i];
                //        console.log("枪的位置" + this.gun.x, this.gun.y);
                //       console.log("母球的位置" + GameControl.motherBall.position);
                this.gun.showLine();
                this.controlGun.open();
            }
            i++;

        }, this);
        this.timertime.start();
    }

    /**与加碰撞产生新球 */
    private createNewBall(ball: p2.Body): void {
        // var newBall = this.createBall();
        // newBall.position = [ball.position[0], ball.position[1]];
        // newBall.mass = 100;
        // this.setBallImpulse(newBall);
    }
    /** 距离右边的距离*/
    private getRight(distance: number): number {
        return GameData.stageWidth - distance;
    }
    /** 距离右边的距离*/
    private getBottom(distance: number): number {
        return GameData.stageHeight - distance;
    }
    /** 传入宽后居中距离*/
    private getCenterX(skinWidth: number): number {
        return (GameData.stageWidth - skinWidth) >> 1;
    }
    /**设置与球的碰撞弹性 */
    public doMaterial(ball: p2.Body): void {
        var world: P2World = this.world;
        var bodys = world.p2World.bodies;
        var l: number = bodys.length;
        for (var i: number = 0; i < l; i++) {
            var body: p2.Body = bodys[i];
            if (body.userData["hit"] || body.userData["motherhit"] || body.userData["bianhit"]) {
                this.setMaterial(ball, body);
            }
        }
    }
    /** 设置刚体碰撞的弹性*/
    private setMaterial(body1: p2.Body, body2: p2.Body): void {
        var materialA: p2.Material = this.material;
        var materialB: p2.Material = this.material;
        body1.shapes[0].material = materialA;
        body2.shapes[0].material = materialB;
        var roleAndStoneMaterial = new p2.ContactMaterial(materialA, materialB, { restitution: 1, friction: 0 });//弹性，摩擦力
        this.world.p2World.addContactMaterial(roleAndStoneMaterial);


    }
    /** 创建底座三角形刚体*/
    private createBottom(): void {
        var world: P2World = this.world;
        var h: number = 59, w: number = 520;//三角形高与宽
        var bottom = world.createConvexBodyShape([[0, h], [w / 2, 0], [w, h]]);
        // var skin:MImage=this.createSkin("bottom_png");
        // bottom.userData.skin=skin;
        //world.drawSkin(bottom);
        bottom.position = [this.getCenterX(0), this.getBottom(h / 2) + 10];
        bottom.type = p2.Body.KINEMATIC;
        this.setAndBallHit(bottom);

        var skin: MImage = this.createSkin("bottom_png");
        skin.x = this.getCenterX(0) - 5;
        skin.y = this.getBottom(h / 2);

        this.addChild(skin);
    }
    /**设置属性后就可以与球碰撞 */
    private setAndBallHit(body: p2.Body): void {
        var shape = body.shapes[0];
        shape.collisionMask = 3;//010与001为0，010与110为1
    }

    /** 创建球刚体*/
    private createBall(): p2.Body {
        var world: P2World = this.world;
        var skin: MImage = this.createSkin("ball_png");
        var body: p2.Body = world.createCircleBodyShape(skin.width >> 1);

        body.userData.skin = skin;
        body.userData["ball"] = true;
        body.mass = 0;//质量设置为0，就可以按规定方向发射，等发生碰撞之后再给球质量
        //body.gravityScale=0;
        body.damping = 0.5;
        var shape = body.shapes[0];
        shape.collisionGroup = 2;//010与001为0，010与110为1
        // shape.collisionMask=2;
        //trace(shape.collisionGroup,shape.collisionMask)
        return body;
    }
    /**创建静态刚体 */
    private createBody2(name: string, shapeType: string = SHAPE.rect, type: number = p2.Body.KINEMATIC): p2.Body {
        var world: P2World = this.world;
        var skin: MImage = this.createSkin(name);
        var body: p2.Body;
        if (shapeType == SHAPE.rect) body = world.createBoxBodyShape(skin.width, skin.height, type);
        else if (shapeType == SHAPE.circle) body = world.createCircleBodyShape(skin.width >> 1, type);
        body.userData.skin = skin;
        body.userData["bianhit"] = true;
        //this.doMaterial(body);
        this.setAndBallHit(body);
        return body;
    }
    /** 创建刚体*/
    private createBody(name: string, shapeType: string = SHAPE.circle, type: number = p2.Body.DYNAMIC): p2.Body {
        var world: P2World = this.world;
        var skin: MImage = this.createSkin(name);
        var body: p2.Body;
        if (shapeType == SHAPE.rect) body = world.createBoxBodyShape(skin.width, skin.height, type);
        else if (shapeType == SHAPE.circle) body = world.createCircleBodyShape(skin.width >> 1, type);
        body.userData.skin = skin;
        this.doMaterial(body);
        this.setAndBallHit(body);
        return body;
    }
    /** 创建带数字刚体*/
    private createNumBody(): void {
        //     var names: string[] = this.names;
        //     var name: string = names[Math.floor(Math.random() * names.length)];
        //     if (this.gun.bulletMax < 5) {//子弹小于5个时增加子弹的概率
        //         if (Math.random() < 0.2) name = ICON_ADD;
        //     } else if (this.gun.bulletMax > 20) {//子弹大于20后减少增加子弹的概率
        //         if (name == ICON_ADD && Math.random() < 0.8) name = "circle1_png";
        //     }
        //   //  var shape = name.split("rect").length == 2 ? SHAPE.rect : SHAPE.circle;
        // var body = this.createBody(name,shape);
        //      return body;
    }
    /** 创建皮肤*/
    private createSkin(name: string): MImage {
        var skin: NumImage = new NumImage(name);
        skin.x = -500;//避免在一出现的时候会在左上角闪现
        skin.setAnchorCenter();
        this.addChild(skin);
        return skin;
    }

    protected loopP2World(): void {
        // var bodys = this.world.p2World.bodies;
        // //traceSimple("bodys",bodys.length);
        // var l: number = bodys.length;
        // for (var i: number = 0; i < l; i++) {
        //     var body: p2.Body = bodys[i];
        //     if (body.userData && body.userData.skin) {
        //         if (body.userData["ball"]) {
        //             var vec = new Point(body.velocity[0], body.velocity[1]);
        //             var vecnum: number = Math.sqrt(vec.x * vec.x + vec.y * vec.y);
        //             if (vecnum < 1) {
        //                 console.log(vecnum);
        //                 this.setBallImpulse(body);
        //             }
        //         }
        //     }
        // }
    }
    protected loop(n: number): boolean {
        var skins: any[] = this.removeSkins;
        // traceSimple(skins.length)
        for (var i: number = 0; i < skins.length; i++) {
            var skin: NumImage = skins[i];
            skin.removeFromParent(true);
        }
        // if (this.ballNum == 0) {
        //     this.nextLevel();
        // }


        return true;
    }
    private nextLevel(): void {
        //   this.ballNum = this.gun.bulletMax;
        this.removeSkins.length = 0;
        this.updateNumBody();
        this.gun.initNum();
    }
}
/**分数图标*/
class NumImage extends MImage {

    // private _value: number = -1;
    // private txt: TextField;
    public constructor(skinName: string = "num") {
        super(skinName);
    }
    // public initNum(v: number): void {
    //     this.txt = this.createText(0, 0);
    //     if (this.skinName == ICON_ADD) {
    //         this._value = 1;
    //     } else {
    //         this.value = v;
    //     }
    // }
    // protected createText(x: number = 0, y: number = 0, s: string = ""): TextField {
    //     var text: TextField = (new moon.Label).textField;
    //     text.x = x; text.y = y; text.text = s;
    //     this.addChild(text);
    //     return text;
    // }
    // set value(v: number) {
    //     this._value = v;
    //     this.txt.text = String(v);
    //     this.txt.textColor = 0;
    //     if (this.parent != null) {
    //         Layout.getIns().setCenterXByPanent(this.txt);
    //         Layout.getIns().setCenterYByPanent(this.txt);
    //     }
    // }
    // get value(): number {
    //     return this._value;
    // }
    public get imageName(): string {
        return this.skinName;
    }
    // public update(): void {
    //     this._value--;
    //     this.value = this._value;
    //     if (this.imageName == ICON_ADD) {
    //         SoundControl.getIns().play(MUSIC_ADDNUM);
    //     } else {
    //         SoundControl.getIns().play(MUSIC_HIT);
    //     }
    // }

}
/**枪*/
class Gun extends moon.BasicView {
    private gun: MImage;
    private txtNum: TextField;
    //  private line: MImage;
    public bulletMax: number = 3;
    private bullet: number;
    public static distance: number;
    private shp: any;
    protected render(): void {
        // var motherBall: GameControl = new GameControl;


        var gun = new MImage("cue_png");
        gun.anchorOffsetX = gun.width >> 1;
        this.addChild(gun);
        this.gun = gun;

        // var numBg: MImage = new MImage("numBg_png");
        // numBg.setAnchorCenter();
        // this.addChild(numBg);

        // var line: MImage = new MImage("line_png");
        // line.anchorOffsetX = line.width >> 1;
        // this.line = line;

        // this.txtNum = this.createText(0, 0, "");
        // this.bullet = this.bulletMax;
        // this.updateNum(this.bullet)
    }


    //画线
    protected onAddToStage_wire() {
        // //画线
        this.shp = new egret.Shape();
        this.shp.graphics.lineStyle(3, 0xC0C0C0);//宽度设置为 2，颜色设置为 0x00ff00 
        this.shp.graphics.moveTo(this.gun.x, this.gun.y);//moveTo 来设定线条的起始点
        this.shp.graphics.lineTo(0, 0);// lineTo 来设定线条的终点
        this.shp.graphics.endFill();
        this.addChild(this.shp);
    }
    //旋转
    // public updatePos(pos: egret.Point): void {
    //     if (-pos.y > this.gun.y) {
    //         this.shp.graphics.lineTo(0, -pos.y);
    //         //    console.log(this.shp.scaleY);
    //         if (this.shp.height > -pos.y) {
    //             this.removeChild(this.shp);
    //             this.shp = new egret.Shape();
    //             // this.shp.alpha = 0;////透明度
    //             var shp: egret.Shape = new egret.Shape();
    //             this.shp.graphics.lineStyle(3, 0x00ff00);
    //             this.shp.graphics.moveTo(this.gun.x, this.gun.y);
    //             this.shp.graphics.lineTo(0, -pos.y);
    //             this.shp.graphics.endFill();
    //             this.addChild(this.shp);
    //         }
    //     }
    //     if (-pos.y < this.gun.y) {
    //         this.shp.graphics.lineTo(0, pos.y);

    //         if (this.shp.height > -pos.y) {
    //             this.removeChild(this.shp);
    //             this.shp = new egret.Shape();
    //             // this.shp.alpha = 0;////透明度
    //             var shp: egret.Shape = new egret.Shape();
    //             this.shp.graphics.lineStyle(3, 0x00ff00);
    //             this.shp.graphics.moveTo(this.gun.x, this.gun.y);
    //             this.shp.graphics.lineTo(0, pos.y);
    //             this.shp.graphics.endFill();
    //             this.addChild(this.shp);
    //         }
    //     }
    //     var r: number = -Math.atan2(pos.x, pos.y) * 180 / Math.PI;
    //     this.gun.rotation = r;
    //     this.shp.rotation = r;
    // }

    public restart(): void {
        // this.bulletMax = 1;
        // this.initNum();
    }
    public updatePos(pos: egret.Point): void {


        Gun.distance = Math.sqrt(this.gun.x * this.gun.x + this.gun.y * this.gun.y);
        //    console.log("pos!!!" + Gun.distance);
        var r: number = -Math.atan2(pos.x, pos.y) * 180 / Math.PI;

        // console.log("pos"+pos.x,pos.y);
        //  if (-pos.y > this.gun.y) {
        //     this.shp.graphics.lineTo(0, -pos.y);
        //     //    console.log(this.shp.scaleY);
        //     if (this.shp.height > -pos.y) {
        //         this.removeChild(this.shp);
        //         this.shp = new egret.Shape();
        //         // this.shp.alpha = 0;////透明度
        //         var shp: egret.Shape = new egret.Shape();
        //         this.shp.graphics.lineStyle(3, 0x00ff00);
        //         this.shp.graphics.moveTo(this.gun.x, this.gun.y);
        //         this.shp.graphics.lineTo(0, -pos.y);
        //         this.shp.graphics.endFill();
        //         this.addChild(this.shp);
        //     }
        // }
        // if (-pos.y < this.gun.y) {
        //     this.shp.graphics.lineTo(0, pos.y);

        //     if (this.shp.height > -pos.y) {
        //         this.removeChild(this.shp);
        //         this.shp = new egret.Shape();
        //         // this.shp.alpha = 0;////透明度
        //         var shp: egret.Shape = new egret.Shape();
        //         this.shp.graphics.lineStyle(3, 0x00ff00);
        //         this.shp.graphics.moveTo(this.gun.x, this.gun.y);
        //         this.shp.graphics.lineTo(0, pos.y);
        //         this.shp.graphics.endFill();
        //         this.addChild(this.shp);
        //     }
        // }
        //  this.line.rotation = r;
        this.gun.rotation = 180 + r;
        //this.shp.rotation =r;

        this.gun.x = -pos.x;
        this.gun.y = -pos.y;


        // var distance_x,distance_y,distance;
        // distance_x=Math.abs(this.gun.x-GameControl.motherBall.position[0]);
        //    console.log("枪x" + this.gun.x + "母球x" + GameControl.motherBall.position[0]);
        // distance_y=Math.abs(this.gun.y-GameControl.motherBall.position[1]);
        //  console.log("y距离"+distance_y+"枪y"+this.gun.y+"母球y"+GameControl.motherBall.position[1]);
        // distance=Math.sqrt(distance_x*distance_x+distance_y*distance_y);
        // console.log("pos!!!"+distance);

        // this.gun.rotation = r; 

        // this.line.rotation =180+r;

    }
    public initNum(): void {
        // this.bullet = this.bulletMax;
        // this.updateNum(this.bullet)
    }
    public showLine(): void {

        //  this.addChildAt(this.line, 0);
        this.addChildAt(this.gun, 0);
        // this.gun.removeFromParent();
        // this.onAddToStage_wire();
    }
    public hideLine(): void {
        //  this.line.removeFromParent();
        this.gun.removeFromParent();
        // this.removeChild(this.shp);
    }
    public updateNum(value: number): void {
        // this.txtNum.text = String(value);
        // this.txtNum.anchorOffsetX = this.txtNum.width >> 1;
        // this.txtNum.anchorOffsetY = this.txtNum.height >> 1;
    }
    public fire(): void {
        //   if (this.bullet > 0) {
        this.updateNum(--this.bullet);
        Tween.get(this.gun).to({ scaleY: 0.6 }, 100).to({ scaleY: 1 }, 100);
        this.dispEvent(EVENT_FIRE);
        //  setTimeout(this.fire.bind(this), 300);
        SoundControl.getIns().play(MUSIC_FIRE);
        //  }
    }
}